<?php

App::uses('AdminController', 'Controller');

class AdminPagesController extends AdminController {
    
    public $uses = array ('Page', 'Category');

    private function _setCommonData() {
        $categories = $this->Category->tree();
        
        $this->set(compact('categories'));
    }
    
    public function index($categoryId=null) {
        $this->_setCommonData();
        
        $this->set('selected_category', $categoryId);
        
        
        $this->set('pages', $this->Page->find('all', array(
            'conditions' => array('Page.category_id' => $categoryId)
        )));
    }
    
    public function create($categoryId) {
        $data = array(
            'category_id' => $categoryId
        );
        $this->set('data', $this->Page->create($data));
        $this->render('form');
    }
    
    public function edit($id) {
        $this->set('data', $this->Page->findById($id));
        $this->render('form');
    }
    
    public function save() {
        $data = $this->data;
        if (! empty($data)) {
            $this->Page->create($data);
            $categoryId = $data['Page']['category_id'];
            
            if (! $this->Page->validates()) {
                $this->setFlash('Uneti podaci nisu validni.');
                $this->set('data', $data);
                return $this->render('form');
            }
            
            if (! $this->Page->save($data)) {
                $this->setFlash('Greška pri čuvanju podataka.');
                $this->set('data', $data);
                return $this->render('form');
            }
            
            $this->setFlash('Stranica je uspešno sačuvana.', 'ok');
            $this->redirect('edit/' . $this->Page->id);
        }
        
        $this->redirect('index');
    }
    
    public function remove($id) {
        $page = $this->Page->findById($id);
        if ($page) {
            $categoryId = $page['Page']['category_id'];
            if ($this->Page->delete($id)) {
                $this->setFlash('Stranica je uspešno obrisana.', 'ok');
            } else {
                $this->setFlash('Greška pri brisanju stranice.');
            }
            
            $this->redirect('index/'.$categoryId);
        }
        $this->redirect('index');
    }
    
    public function save_category() {
        $this->autoRender = false;
        $data = $this->data;
        
        $error = 'Ništa za sačuvati.';
        if (! empty($data)) {
            if (empty($data['key'])) {
                $data['key'] = Inflector::slug(strtolower($data['name']), '-');
            }
            if ($this->Category->save($data)) {
                $this->setFlash('Kategorija je uspešno sačuvana.', 'ok');
                die('Success');
            }
            
            $error = 'Greška pru čuvanju podataka.';
        }
        
        header('HTTP/1.0 404 Error');
        die($error);
    }
    
    public function delete_category($id) {
        $this->autoRender = false;
        if (
            $this->Category->delete($id) &&
            $this->Category->deleteAll(array('Category.parent_id'=>$id)) &&
            $this->Page->deleteAll(array('Page.category_id'=>$id))
        ) {
            $this->setFlash('Kategorija je uspešno obrisana.', 'ok');
            die('Success');
        }
        
        header('HTTP/1.0 404 Error');
        die('Greška pri brisanju kategorije.');
    }
    
}
